package com.mapapp.flutter_mapapp.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by zhaojy on 2018-05-15.
 */

public class StringUtils {

    private static final Map<String, String> dayMap = new HashMap<>();

    static {
        dayMap.put("1", "一");
        dayMap.put("2", "二");
        dayMap.put("3", "三");
        dayMap.put("4", "四");
        dayMap.put("5", "五");
        dayMap.put("6", "六");
        dayMap.put("7", "日");
    }


    public static boolean isEmpty(CharSequence s) {
        return s == null || s.length() == 0;
    }

    public static boolean isNotEmpty(CharSequence s) {
        return !isEmpty(s);
    }

    public static boolean equals(CharSequence a, CharSequence b) {
        if (a == b) return true;
        int length;
        if (a != null && b != null && (length = a.length()) == b.length()) {
            if (a instanceof String && b instanceof String) {
                return a.equals(b);
            } else {
                for (int i = 0; i < length; i++) {
                    if (a.charAt(i) != b.charAt(i)) return false;
                }
                return true;
            }
        }
        return false;
    }

    public static boolean notEquals(CharSequence a, CharSequence b) {
        return !equals(a, b);
    }

    public static List<String> toList(String source, String regex) {
        if (isEmpty(source)) {
            return new ArrayList<>();
        }
        if (!source.contains(regex)) {
            return Collections.singletonList(source);
        }
        return Arrays.asList(source.split(regex));
    }

    public static String numberToChinese(String number) {
        return dayMap.get(number);
    }

    public static String join(List<String> list, String regex){
        StringBuilder sb=new StringBuilder();
        for(int i = 0; i < list.size(); i ++){
            if(i == (list.size() - 1)){
                sb.append(list.get(i));
            }else{
                sb.append(list.get(i)).append(regex);
            }
        }
        return sb.toString();
    }

    public static boolean isPhoneNumberValid(String phone) {
        return !isEmpty(phone) && phone.length() == 11 && phone.startsWith("1");
    }

    public static boolean isPhoneNumberInvalid(String phone) {
        return !isPhoneNumberValid(phone);
    }

    public static String randomString(int length) {
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<length; i++){
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

}
